package com.github.wj.web.service.impl;

import com.github.wj.common.security.util.DateFormatUtils;
import com.github.wj.common.security.util.HttpFileUploadUtil;
import com.github.wj.security.server.controller.BaseService;
import com.github.wj.web.domain.StudentRegisterDomain;
import com.github.wj.web.entity.Gxxs0101Bm;
import com.github.wj.web.entity.LayerUIMessage;
import com.github.wj.web.entity.ResultMessage;
import com.github.wj.web.entity.TSjzxDmjzx;
import com.github.wj.web.service.CustomDmjzxService;
import com.github.wj.web.service.CustomLborganizationService;
import com.github.wj.web.service.StudentRegisterService;
import com.jsoniter.output.JsonStream;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.IOException;
import java.text.ParseException;
import java.util.*;

/**
 * Create by IntelliJ IDEA
 * 用户：王建
 * 日期：2018/3/13
 */
@Service
public class StudentRegisterServiceImpl extends BaseService implements StudentRegisterService {

    @Value("${upload.path:D:/uploaded_files/}")
    private String path;

    @Autowired
    private StudentRegisterDomain domain;

    @Autowired
    private CustomDmjzxService service;

    @Autowired
    private CustomLborganizationService lborganizationService;

    @Override
    public void registerIndex(ModelMap map) {
        map.put("List_xb", service.getAllBySsdm("GBT_XBDM"));//性别代码
        map.put("list_mz", service.getAllBySsdm("GBT_MZDM"));//名族
        map.put("list_zzmm", service.getAllBySsdm("GBT_ZZMM"));//政治面貌
        map.put("list_sss", service.getAllBySSdmAndSsdm("GBT_XZQH", "%0000"));//所属省份
        map.put("list_ssy",service.getAllBySSdmAndSsdm("GBT_XZQH","34%00"));
        List<TSjzxDmjzx> list=service.getAllBySSdmAndSsdm("GBT_XZQH","3412%");
        List<TSjzxDmjzx> list1=new ArrayList<>();
        if(null!=list&&list.size()>0){
            for (TSjzxDmjzx sjzxDmjzx:list){
                if(!"341200".equals(sjzxDmjzx.getSsdm())){
                    list1.add(sjzxDmjzx);
                }
            }
        }
        map.put("list_yyy",list1);
        map.put("list_yx", lborganizationService.getAllList());//院系
    }

    @Override
    public String uploadFile(MultipartHttpServletRequest request) {
        LayerUIMessage message = new LayerUIMessage();
        MultipartFile multipartFile = request.getFile("file");
        String oldFileName = multipartFile.getOriginalFilename();
        try {
            String newFileName = HttpFileUploadUtil.uploadUtil(multipartFile.getInputStream(), path, oldFileName);
            if (StringUtils.isNotBlank(newFileName)) {
                Map<String, String> map = new HashMap<>();
                map.put("src", "/static/upload/" + newFileName);
                message.setData(map);
                message.setMsg("上传成功！");
            } else {
                message.setMsg("上传失败！");
                message.setCode(1);
            }
            String msg = JsonStream.serialize(message);
            return msg;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean save(ModelMap modelMap, Gxxs0101Bm gxxs0101Bm) {
        if(null!=gxxs0101Bm&&StringUtils.isNotBlank(gxxs0101Bm.getSfzjh())){
            List<Gxxs0101Bm> list=domain.findAllBySfzjh(gxxs0101Bm.getSfzjh());
            if(null!=list&&list.size()>0){
                return false;
            }
            else {
                gxxs0101Bm.setId(sequenceService.returnId("GXXS0101_BM"));
                if(StringUtils.isNotBlank(gxxs0101Bm.getCsrqFmt())){
                    try {
                        Date date= DateFormatUtils.parse(gxxs0101Bm.getCsrqFmt(),"yyyy-MM-dd");
                        String dates=DateFormatUtils.formatDate(date,"yyyyMMdd");
                        gxxs0101Bm.setCsrq(Long.parseLong(dates));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }
                Calendar calendar=Calendar.getInstance();//获取时间
                Long bmnf=Long.valueOf(calendar.get(Calendar.YEAR));//强制类型转化
                gxxs0101Bm.setBmsj(new Date());//报名时间
                gxxs0101Bm.setJfzt(0l);//缴费状态
                gxxs0101Bm.setBmnf(bmnf);//报名年份

                domain.save(gxxs0101Bm);
                return true;
            }
        }
        return false;
    }
}
